/*
 *
 *单向链表
 *
 *
 */
#include "Linked_list.h"
#define BUF_SIZ 5


int main(){
		List Nodes = NULL; 
		Nodes = (List)malloc(sizeof(List));
		Nodes->Next = NULL;
		int i = 0;
		//
		//初始化链表
		for(;i<BUF_SIZ;++i){
				ElementT Node = (ElementT)malloc(sizeof(ElementT));
				printf("输入第%d个节点的数据\n用户名:",i+1);
				Node->Name = (char *)malloc(sizeof(char)*10);
				scanf("%s",Node->Name);
				printf("用户id:");
				scanf("%d",&Node->Userid);
				Add(Node,Nodes);
		}
		Print(Nodes);

		ElementT Temp = (ElementT)malloc(sizeof(ElementT));
		Temp->Userid = 3;
		Temp->Name = (char *)malloc(sizeof(char)*10);
		strcpy(Temp->Name,"王刚");
		Insert(Temp,Nodes,Nodes->Next->Next->Next);
		Print(Nodes);
		/*
		ElementT Cmp = (ElementT)malloc(sizeof(ElementT));
		Cmp->Name = (char *)malloc(sizeof(5));
		strcpy(Cmp->Name,"王五");
		Cmp->Userid = 4;
		Position Result = Find(Cmp,Nodes);
		printf("********Find查找结果*********\n");
		if(NULL!=Result)
				printf("%d----->%s\n",Result->Element->Userid,Result->Element->Name);
		else
				printf("没有找到!!\n");


		Position Preresult = FindPrevious(Cmp,Nodes);
		printf("********FindPreviousd查找结果*********\n");
		if(NULL!=Preresult)
				printf("%d----->%s\n",Preresult->Element->Userid,Preresult->Element->Name);
		else
				printf("没有找到!!\n");


		printf("%s\n",IsEmpty(Nodes)?"链表为空":"链表不为空");
		Delete(Cmp,Nodes);
		Print(Nodes);
		*/

		return 0;
		
}

List MakeEmpty(List Head){
		List Node = (List)malloc(sizeof(List));
		Node->Element = (ElementT)malloc(sizeof(ElementT));
		Node->Element = NULL;
		Node->Next = NULL;
		Head->Next = Head;
		return Node;
}

int IsEmpty(List T){
	return T == NULL;	
}

int IsLast(Position P,List L){
		while(L->Next!=NULL) L = L->Next;
		return P == L;
}

void Delete(ElementT X,List L){
		List Temp = NULL;
		List Target = FindPrevious(X,L);
		if(!IsLast(Target,L)){
				Temp = Target->Next;
				Target->Next = Temp->Next;
		}
		free(Temp);
}

void Add(ElementT X,List L){
		Position P = L;
		while(P->Next != NULL)
				P = P->Next;
		List Node_new = (List)malloc(sizeof(List));
		Node_new->Element = X;
		Node_new->Next = NULL;
		P->Next = Node_new;
}

void Print(List Nodes){
		List Temp = Nodes->Next;
		while(Temp!=NULL){
				printf("%s---->%d\n",Temp->Element->Name,Temp->Element->Userid);
				Temp = Temp->Next;
		}
		printf("NULL\n");
}

void Insert(ElementT X,List L,Position P){
		Position TempCell = NULL ,Addition = NULL;
		TempCell = (Position)malloc(sizeof(Position));
		TempCell = FindPrevious(P->Element,L);
		Addition = (Position)malloc(sizeof(Position));
		Addition->Element = X;
		Addition->Next = P;
		TempCell->Next = Addition;
}

//查找函数:Find 
Position Find(ElementT X,List L){
		Position P;
		P = L->Next;
		while(P->Next!=NULL && 0 != strcmp(P->Element->Name,X->Name) && (P->Element->Userid != X->Userid))
				P = P->Next;
		return P;

}

//查找前序函数:FindPrevious
Position FindPrevious(ElementT X, List L){
		Position P;	
		P = L;
		while(P->Next != NULL && P->Next->Element->Userid!=X->Userid && 0 != strcmp(P->Next->Element->Name,X->Name))
				P = P->Next;
		return P;
}
